package com.iias.communication.util;

public class LongToByte {

    /**
     * 把一个4位的数组转化位整形
     *
     * @param value
     * @return
     * @throws Exception
     */
    public static long fourBytesToLong(byte[] value) {
        // if (value.length < 4) {
        // throw new Exception("Byte array too short!");
        // }
        int temp0 = value[0] & 0xFF;
        int temp1 = value[1] & 0xFF;
        int temp2 = value[2] & 0xFF;
        int temp3 = value[3] & 0xFF;
        return (((long) temp0 << 24) + (temp1 << 16) + (temp2 << 8) + temp3);
    }

    /**
     * 把一个长整形改为byte数组
     *
     * @param bytes
     * @return
     * @throws Exception
     */
    public static long bytesToLong(byte[] bytes) {

        long value = 0;
        int temp ;
        for (int i = 0; i < bytes.length; i++) {
            temp = (bytes.length - 1 - i) * 8;

            if (temp == 0){
                value += bytes[i] & 0xFF;
            }else {
                value += (bytes[i] & 0x0ff) << temp;
            }
        }

        return value;

//        byte[] result = new byte[len];
//        int temp;
//        for (int i = 0; i < len; i++) {
//            temp = (len - 1 - i) * 8;
//            if (temp == 0) {
//                result[i] += (value & 0x0ff);
//            } else {
//                result[i] += (value <<< temp) & 0x0ff;
//            }
//        }
//        return result;
    }
    /**
     * 把一个数组转化长整形
     *
     * @param value
     * @return
     * @throws Exception
     */
    public static long bytes2Long(byte[] value) {
        long result = 0;
        int len = value.length;
        int temp;
        for (int i = 0; i < len; i++) {
            temp = (len - 1 - i) * 8;
            if (temp == 0) {
                result += (value[i] & 0x0ff);
            } else {
                result += (value[i] & 0x0ff) << temp;
            }
        }
        return result;
    }

    /**
     * 把一个长整形改为byte数组
     *
     * @param value
     * @return
     * @throws Exception
     */
    public static byte[] longToBytes(long value){
        return longToBytes(value, 8);
    }

    /**
     * 把一个长整形改为byte数组
     *
     * @param value
     * @return
     * @throws Exception
     */
    public static byte[] longToBytes(long value, int len) {
        byte[] result = new byte[len];
        int temp;
        for (int i = 0; i < len; i++) {
            temp = (len - 1 - i) * 8;
            if (temp == 0) {
                result[i] += (value & 0x0ff);
            } else {
                result[i] += (value >>> temp) & 0x0ff;
            }
        }
        return result;
    }


    public static byte[] LongToByte(long lg) {
        byte[] b = new byte[8];
        int c = 0;
        for (int i = 0; i <8 ; i++) {
            b[i] = (byte)(lg >> c);
            c = c + 8;
        }
        return b;
    }

    public static long ByteToLong(byte[] b) {
        long lg = 0;
        int c = 0;
        for (int i = 0; i < b.length; i++)
        {
            lg = lg+(b[i] << c);
            c = c + 8;
        }
        return lg;
    }
}
